﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GTA
{
    public partial class FrmTaskComments : BaseForm
    {
        #region Local Objects
        private BusinessEntity.Task task;
        #endregion

        #region Starting Form
        public FrmTaskComments(BusinessEntity.Task task)
        {
            InitializeComponent();
            this.task = task;
        }
        private void FrmTaskComments_Load(object sender, EventArgs e)
        {
            StartForm();
        }
        private void StartForm()
        {
            if (task != null)
            {
                txtTaskID.Text = task.ID.ToString();
                txtTitle.Text = task.Title;
                LoadTaskComment();
            }
            else
            {
                MessageBox.Show("Task não identificada", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }
        #endregion

        #region Events
        private void cmdSave_Click(object sender, EventArgs e)
        {
            SaveTaskComment();
        }
        #endregion

        #region Methods
        private void LoadTaskComment()
        {
            try
            {
                task.Comment = new BusinessLogic.GTA().GetTaskComment(task.ID, Program.LoggedUser.ID);
                txtComment.Text = task.Comment != null ? task.Comment.Comment : String.Empty;
                txtComment.Focus();
            }
            catch (Exception ex)
            {
                HandleException("Carregar os comentários da task", ex);
            }
        }
        private void SaveTaskComment()
        {
            try
            {
                if (task.Comment == null)
                    task.Comment = new GTA.BusinessEntity.TaskComment() { Comment = txtComment.Text };
                else
                    task.Comment.Comment = txtComment.Text;
                new BusinessLogic.GTA().SaveTaskComment(task, Program.LoggedUser.ID);
                MessageBox.Show("Comentário salvo com sucesso!", "Confirmação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LoadTaskComment();
                txtComment.Focus();
                txtComment.SelectionStart = txtComment.Text.Length;
            }
            catch (Exception ex)
            {
                HandleException("Salvar o comentário da task", ex);
            }
        }
        #endregion
    }
}
